{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------\n",
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-18\n",
    "* week07\n",
    "* [参考的文档](https://python.org/doc)\n",
    "* [许老师博客](https://cnblogs.com/xuzhichao)\n",
    "\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 回顾上周内容：\n",
    "\n",
    "## 认识列表 ：[value1,value2,value3...]\n",
    "\n",
    "## 列表的查询： \n",
    "\n",
    "> 1. 索引值进行查询 （单个值）：index\n",
    "> 2. 切片进行查询（多值）：slice\n",
    "\n",
    "## 列表的一般方法\n",
    "\n",
    "> 1. 增加元素\n",
    ">> 1. append() 增加元素到列表到末尾: L.append(object)\n",
    ">> 2. insert() 指定位置增加元素 : L.insert(index)\n",
    "> 2. 删除元素\n",
    ">> 1. remove() 按照 值 进行删除 : L.remove(value) \n",
    ">> 2. pop()按照 索引值 进行删除： L.pop(index) 注意：不写index 默认删除列表的最后一个元素\n",
    "> 3. 修改内容\n",
    "> 4. 查询内容（参考上述2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 1. clear()\n",
    ">> 1. 使用方法：L.clear()\n",
    ">> 2. 结果： remove all items from L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear(...) method of builtins.list instance\n",
      "    L.clear() -> None -- remove all items from L\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 2. count()\n",
    ">> 1. 使用方法 ： L.count(value)\n",
    ">> 2. 结果： 查询某个value出现的次数（共现的次数），通常用在计算词频、统计操作当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function count:\n",
      "\n",
      "count(...) method of builtins.list instance\n",
      "    L.count(value) -> integer -- return number of occurrences of value\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list = [88,89,68,78,88,90,93,88]\n",
    "分数_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 3. extend() 扩展\n",
    ">> 1. 使用方法: L.extend(iterable)\n",
    ">> 2. 结果: iterable 将拼接在 L 的后面（末尾）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班 = [88, 89, 68, 78, 88, 90, 93, 88]\n",
    "分数_list_数媒技术2班 = [88,78,76,77,88,89,77,66]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(...) method of builtins.list instance\n",
      "    L.extend(iterable) -> None -- extend list by appending elements from the iterable\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.extend(分数_list_数媒技术2班)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等价于\n",
    "[88, 89, 68, 78, 88, 90, 93, 88] + [88,78,76,77,88,89,77,66]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 4. <font style=\"color:red\">**copy()**</font>\n",
    ">> 1. 在列表中，\"赋值\" 不等价于 \"复制\" \n",
    ">> 2. 真正的复制列表的两种常见方法\n",
    ">>> 1. copy()\n",
    ">>> 2. 切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack Alex\n",
      "140454685575240 140454685573336\n",
      "name1 和 name2 拥有不同的数据地址\n"
     ]
    }
   ],
   "source": [
    "name1 = \"Alex\"\n",
    "\n",
    "name2 = name1 \n",
    "\n",
    "name1 = \"Jack\"\n",
    "\n",
    "print(name1,name2)\n",
    "print(id(name1),id(name2))\n",
    "print(\"name1 和 name2 拥有不同的数据地址\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 结论是： 赋值有两种情况：\n",
    "> 1. 赋值的数据类型是 不可变的数据 （字符串、数值、元组），分配不同的数据地址\n",
    "> 2. 赋值的数据类型是 可变的数数据 （列表） 分配同一个地址，指向同一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#此案例 来源：课本P72 : 看起来像复制，但其实不是\n",
    "first = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140454682100488\n"
     ]
    }
   ],
   "source": [
    "print(id(first))  # first 的 数据地址： 140454682100488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "second = first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140454682100488\n"
     ]
    }
   ],
   "source": [
    "print(id(second)) # second 的 数据地址： 140454682100488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的复制（方法一）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "140454685541448 140454678191496\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的复制（方法二）\n",
    "* 切片[:] 可以实现真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "140454681318344 140454687757064\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first[:] \n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 思考 copy() 只复制 第一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]]\n",
      "140454685638344 140454678191304\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,[4,5,6]]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "first[3].append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
